package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.DmcRfidLabelRoleMapper;
import com.csun.cmny.provider.exceptions.DmcBizException;
import com.csun.cmny.provider.model.domain.DmcRfidLabelOldman;
import com.csun.cmny.provider.model.domain.DmcRfidLabelRole;
import com.csun.cmny.provider.model.dto.rfid.RfidLabelRolePageQuery;
import com.csun.cmny.provider.model.enums.rfid.DmcRfidLabelRoleCodeEnum;
import com.csun.cmny.provider.model.vo.rfid.RfidLabelRoleVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.DmcRfidLabelOldmanService;
import com.csun.cmny.provider.service.DmcRfidLabelRoleService;
import com.csun.cmny.provider.utils.TreeUtil;
import com.csun.cmny.util.PublicUtil;
import com.github.pagehelper.PageHelper;
import com.google.common.base.Preconditions;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-12-07 09:21
 * @Description:
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class DmcRfidLabelRoleServiceImpl extends BaseService<DmcRfidLabelRole> implements DmcRfidLabelRoleService {

    @Resource
    private CmnyMessageSource cmnyMessageSource;
    @Resource
    private DmcRfidLabelRoleMapper dmcRfidLabelRoleMapper;
    @Resource
    private DmcRfidLabelOldmanService dmcRfidLabelOldmanService;

    /**
     * query all dmc rfid label role
     *
     * @return the list
     */
    @Override
    public List<RfidLabelRoleVo> queryAll(Long groupId) {

        List<RfidLabelRoleVo> rfidLabelRoleVoList = dmcRfidLabelRoleMapper.queryAll(groupId);

        return TreeUtil.getChildLabelRoleVos(rfidLabelRoleVoList, 0L);
    }

    /**
     * add dmc rfid label role
     *
     * @param dmcRfidLabelRole
     * @return
     */
    @Override
    public Integer add(DmcRfidLabelRole dmcRfidLabelRole, LoginAuthDto loginAuthDto) {

        // 校验pid
        Preconditions.checkArgument(dmcRfidLabelRole.getPid() != null,
                cmnyMessageSource.getMessage("rfid.label.role.pid.null", null));
        if (!dmcRfidLabelRole.getPid().equals(0l)) {
            // 校验是否存在
            DmcRfidLabelRole parent = dmcRfidLabelRoleMapper.selectByPrimaryKey(dmcRfidLabelRole.getPid());
            if (parent == null) {
                throw new DmcBizException(ErrorCodeEnum.DMC100612005);
            }
            parent.setLeaf(1);
            dmcRfidLabelRoleMapper.updateByPrimaryKeySelective(parent);

            dmcRfidLabelRole.setLevel(parent.getLevel() + 1);
        }

        // 校验code
        Preconditions.checkArgument(PublicUtil.isNotEmpty(dmcRfidLabelRole.getRoleCode()),
                cmnyMessageSource.getMessage("rfid.label.role.code.null", null));

        // 绑定角色的code是否合法
        DmcRfidLabelRoleCodeEnum roleEnum = DmcRfidLabelRoleCodeEnum.getEnum(dmcRfidLabelRole.getRoleCode());
        if (PublicUtil.isEmpty(roleEnum)) {
            throw new DmcBizException(ErrorCodeEnum.DMC100612004);
        }

        // 校验name
        Preconditions.checkArgument(PublicUtil.isNotEmpty(dmcRfidLabelRole.getRoleName()),
                cmnyMessageSource.getMessage("rfid.label.role.name.null", null));

        dmcRfidLabelRole.setGroupId(loginAuthDto.getGroupId());
        dmcRfidLabelRole.setUpdateInfo(loginAuthDto);

        return dmcRfidLabelRoleMapper.insertSelective(dmcRfidLabelRole);
    }

    /**
     * edit dmc rfid label role
     *
     * @param dmcRfidLabelRole
     * @return
     */
    @Override
    public Integer edit(DmcRfidLabelRole dmcRfidLabelRole, LoginAuthDto loginAuthDto) {

        // 校验id
        Preconditions.checkArgument(PublicUtil.isNotEmpty(dmcRfidLabelRole.getId()),
                cmnyMessageSource.getMessage("rfid.label.role.id.null", null));

        // 检验code
        if (PublicUtil.isNotEmpty(dmcRfidLabelRole.getRoleCode())) {

            /**
             * @date    2019-12-12
             * @author  chisj
             * @desc    如果门禁卡标签已经绑定了，不能修改code
             */
            DmcRfidLabelOldman dmcRfidLabelOldman = new DmcRfidLabelOldman();
            dmcRfidLabelOldman.setRoleId(dmcRfidLabelRole.getId());
            int count = dmcRfidLabelOldmanService.selectCount(dmcRfidLabelOldman);
            if (count > 0) {
                throw new DmcBizException(ErrorCodeEnum.DMC100612006);
            }

            // 绑定角色的code是否合法
            DmcRfidLabelRoleCodeEnum roleEnum = DmcRfidLabelRoleCodeEnum.getEnum(dmcRfidLabelRole.getRoleCode());
            if (PublicUtil.isEmpty(roleEnum)) {
                throw new DmcBizException(ErrorCodeEnum.DMC100612004);
            }
        }
        dmcRfidLabelRole.setUpdateInfo(loginAuthDto);

        return dmcRfidLabelRoleMapper.updateByPrimaryKeySelective(dmcRfidLabelRole);
    }

    /**
     * delete dmc rfid label role by id
     *
     * @param id
     * @return
     */
    @Override
    public Integer deleteById(Long id) {

        DmcRfidLabelRole dmcRfidLabelRole = dmcRfidLabelRoleMapper.selectByPrimaryKey(id);
        if (dmcRfidLabelRole == null) {
            throw new DmcBizException(ErrorCodeEnum.DMC100612005);
        }

        /**
         * @date    2019-12-13
         * @author  chisj
         * @desc    如果有子角色，则不能删除
         */
        DmcRfidLabelRole child = new DmcRfidLabelRole();
        child.setPid(id);
        int count = dmcRfidLabelRoleMapper.selectCount(child);
        if (count > 0) {
            throw new DmcBizException(ErrorCodeEnum.DMC100612008);
        }

        if (!dmcRfidLabelRole.getPid().equals(0l)) {
            DmcRfidLabelRole parent = new DmcRfidLabelRole();
            parent.setId(dmcRfidLabelRole.getPid());
            count = dmcRfidLabelRoleMapper.selectCount(parent);
            if (count == 0) {
                parent.setLeaf(0);  // 不是叶子节点
            }
            dmcRfidLabelRoleMapper.updateByPrimaryKeySelective(parent);
        }

        /**
         * @date    2019-12-12
         * @author  chisj
         * @desc    如果门禁卡标签已经绑定了，不能修改code
         */
        DmcRfidLabelOldman dmcRfidLabelOldman = new DmcRfidLabelOldman();
        dmcRfidLabelOldman.setRoleId(dmcRfidLabelRole.getId());
        count = dmcRfidLabelOldmanService.selectCount(dmcRfidLabelOldman);
        if (count > 0) {
            throw new DmcBizException(ErrorCodeEnum.DMC100612006);
        }

        return dmcRfidLabelRoleMapper.deleteByPrimaryKey(id);
    }

    /**
     * query dmc rfid label role list wiht page
     *
     * @param rfidLabelRolePageQuery the rfid label role page query
     *
     * @return the list
     */
    @Override
    public List<RfidLabelRoleVo> queryListWithPage(RfidLabelRolePageQuery rfidLabelRolePageQuery) {

        PageHelper.startPage(rfidLabelRolePageQuery.getPageNum(), rfidLabelRolePageQuery.getPageSize());

        return dmcRfidLabelRoleMapper.queryListWithPage(rfidLabelRolePageQuery);
    }
}
